import os
import sys

from PyQt6.QtCore import QSize, Qt, pyqtSignal
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QPushButton, QDialogButtonBox, QWidget, \
    QHBoxLayout


class ConfirmDialog(QDialog, ):
    clicked = pyqtSignal()

    def __init__(self, path, on_delete):
        super().__init__()
        self.path = path
        self.on_delete = on_delete

        layout = QVBoxLayout()

        button_box = QDialogButtonBox(self)
        button_box.addButton('Close', QDialogButtonBox.ButtonRole.AcceptRole)
        button_box.accepted.connect(self.accept)  # 点击Yes时接受对话框
        button_box.addButton('Delete', QDialogButtonBox.ButtonRole.RejectRole)
        button_box.rejected.connect(self.delete_file)  # 点击Yes时接受对话框
        layout.addWidget(button_box)

        img_iv = QLabel(self)
        layout.addWidget(img_iv)
        photo = QPixmap(path)
        photo = photo.scaled(photo.size() * 0.5, Qt.AspectRatioMode.IgnoreAspectRatio)
        img_iv.setPixmap(photo)

        self.setLayout(layout)
        self.clicked.connect(self.get_result)

    def delete_file(self):
        if os.path.exists(self.path):
            os.remove(self.path)
        self.on_delete()
        self.accept()

    def get_result(self):
        return self.exec() == QDialog.DialogCode.Accepted  # 返回True表示用户点击了Yes


if __name__ == "__main__":
    app = QApplication(sys.argv)

    confirm_dialog = ConfirmDialog("ok")
    result = confirm_dialog.get_result()

    if result:
        print("用户选择了继续")
    else:
        print("用户取消了操作")

    sys.exit(app.exec())
